package FrameN;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.Caret;

import net.miginfocom.swing.MigLayout;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemListener;
import java.awt.event.MouseListener;

import javax.swing.SwingConstants;

import org.apache.poi.hssf.record.formula.functions.Index;
import org.pushingpixels.substance.api.SubstanceLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceSaharaLookAndFeel;
import org.pushingpixels.substance.internal.painter.BackgroundPaintingUtils;

import check_form.CheckString;

import danhmuc.KhachHang;
import form.DSJ_FormMuaHang;
import form.DSJ_KhachHang;
import form.DSJ_NhanVien;
import form.FormBanHang;

import java.awt.FlowLayout;
import java.util.Vector;

import jxl.write.Font;


public class FNKhachHang extends JDialog {
	private static final FocusListener BackgroundPaintingUtils = null;
	private KhachHang khachhang;
	private JPanel contentPane;
	private JTextField tfmaKH;
	private JTextField tftenKH;
	private JTextField tfdiachi;
	private JTextField tfphone;
	private JTextField tffax;
	private JTextField tfemail;
	private JLabel lblSTiKhon;
	private JTextField tfsotaikhoan;
	private JLabel lblNgnHng;
	private JComboBox comboBox;
	private JComboBox comboBox1;
	private JPanel panel;
	private JButton btnOK;
	private JButton btnCancel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                JFrame.setDefaultLookAndFeelDecorated(true);
                LookAndFeel laf = new SubstanceSaharaLookAndFeel();
                SubstanceLookAndFeel.setToUseConstantThemesOnDialogs(true);
                //UIManager.put(SubstanceLookAndFeel.TABBED_PANE_CLOSE_BUTTONS_PROPERTY,Boolean.TRUE);
                UIManager.put(SubstanceLookAndFeel.SHOW_EXTRA_WIDGETS, Boolean.TRUE);
                JFrame.setDefaultLookAndFeelDecorated(false);
                JDialog.setDefaultLookAndFeelDecorated(true);
                try {
                        UIManager.setLookAndFeel(laf);
    					FNKhachHang frame = new FNKhachHang(null);
    					frame.setVisible(true);
                } catch (UnsupportedLookAndFeelException e) {
                        e.printStackTrace();
                }
            }
        });
		
	}

	/**
	 * Create the frame.
	 */
	public FNKhachHang(KhachHang khachhang) {
		this.setTitle("Thêm Thông Tin Khách Hàng");
		this.setModal(true);
		setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
		Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		   setBounds((screenSize.width-483)/2, (screenSize.height-300)/2, 483, 300);
		//setBounds(100, 100, 483, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new MigLayout("", "[][grow][][grow]", "[30][30][30][30][30][30][grow]"));
		
		JLabel lblMKh = new JLabel("Mã KH:");
		contentPane.add(lblMKh, "cell 0 0,alignx trailing");
		
		tfmaKH = new JTextField();
		contentPane.add(tfmaKH, "cell 1 0,growx");
		tfmaKH.setColumns(10);
		tfmaKH.setEditable(false);
		
		JLabel lblTnKh = new JLabel("Tên KH: ");
		contentPane.add(lblTnKh, "cell 2 0,alignx trailing");
		
		tftenKH = new JTextField();
		contentPane.add(tftenKH, "cell 3 0,growx");
		tftenKH.setColumns(10);
		tftenKH.addCaretListener(new CaretListener() {
			
			@Override
			public void caretUpdate(CaretEvent arg0) {
				// TODO Auto-generated method stub
				tftenKH.setBackground(Color.WHITE);
			}
		});
		
		JLabel lblaCh = new JLabel("Địa Chỉ:");
		contentPane.add(lblaCh, "cell 0 1,alignx trailing");
		
		tfdiachi = new JTextField();
		contentPane.add(tfdiachi, "cell 1 1 3 1,growx");
		tfdiachi.setColumns(10);
		tfdiachi.addCaretListener(new CaretListener() {
			
			@Override
			public void caretUpdate(CaretEvent arg0) {
				// TODO Auto-generated method stub
				tfdiachi.setBackground(Color.WHITE);
			}
		});
		
		JLabel lblPhone = new JLabel("Phone:");
		contentPane.add(lblPhone, "cell 0 2,alignx trailing");
		
		tfphone = new JTextField();
		contentPane.add(tfphone, "cell 1 2,growx");
		tfphone.setColumns(10);
		tfphone.addCaretListener(new CaretListener() {
			
			@Override
			public void caretUpdate(CaretEvent arg0) {
				// TODO Auto-generated method stub
				tfphone.setBackground(Color.WHITE);
			}
		});
		
		JLabel lblFax = new JLabel("Fax:");
		contentPane.add(lblFax, "cell 2 2,alignx trailing");
		
		tffax = new JTextField();
		contentPane.add(tffax, "cell 3 2,growx");
		tffax.setColumns(10);
		tffax.addCaretListener(new CaretListener() {
			
			@Override
			public void caretUpdate(CaretEvent arg0) {
				// TODO Auto-generated method stub
				tffax.setBackground(Color.WHITE);
			}
		});
		
		JLabel lblEmail = new JLabel("Email:");
		contentPane.add(lblEmail, "cell 0 3,alignx trailing");
		
		tfemail = new JTextField();
		contentPane.add(tfemail, "cell 1 3,growx");
		tfemail.setColumns(10);
		tfemail.addCaretListener(new CaretListener() {
			
			@Override
			public void caretUpdate(CaretEvent arg0) {
				// TODO Auto-generated method stub
				tfemail.setBackground(Color.WHITE);
			}
		});
		lblSTiKhon = new JLabel("Số Tài khoản:");
		contentPane.add(lblSTiKhon, "cell 0 4,alignx trailing");
		
		tfsotaikhoan = new JTextField();
		contentPane.add(tfsotaikhoan, "cell 1 4 2 1,growx");
		tfsotaikhoan.setColumns(10);
		tfsotaikhoan.addCaretListener(new CaretListener() {
			
			@Override
			public void caretUpdate(CaretEvent arg0) {
				// TODO Auto-generated method stub
				tfsotaikhoan.setBackground(Color.WHITE);
			}
		});
		
		lblNgnHng = new JLabel("Ngân Hàng:");
		contentPane.add(lblNgnHng, "cell 0 5,alignx trailing");
		
		comboBox = new JComboBox();
		comboBox1 = new JComboBox();
		contentPane.add(comboBox, "cell 1 5,growx");
		
		panel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		contentPane.add(panel, "cell 3 6,grow");
		
		btnOK = new JButton("OK");
		btnOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {					
				if(e.getSource() == btnOK){
					int a = 0 ;
					comboBox1.setSelectedIndex(comboBox.getSelectedIndex());
					
					if(CheckString.Onlytext(tftenKH.getText(), 50, 3)==false){					
						JOptionPane.showMessageDialog(btnOK, "Lỗi nhập tên Khách hàng ! \n Vui lòng nhập lại.","Lỗi",JOptionPane.OK_OPTION);
						tftenKH.setText("");
						tftenKH.setBackground(Color.red);
						 a =1;
					}
					if(CheckString.Address(tfdiachi.getText(), 50, 3)==false && a == 0){					
						JOptionPane.showMessageDialog(btnOK, "Lỗi nhập địa chỉ ! \n Vui lòng nhập lại.","Lỗi",JOptionPane.OK_OPTION);
						tfdiachi.setText("");
						tfdiachi.setBackground(Color.red);
						 a =1;
					}
					if(CheckString.PhoneNumber(tfphone.getText())==false && a == 0){					
						JOptionPane.showMessageDialog(btnOK, "Lỗi nhập số điện thoại ! \n Vui lòng nhập lại.","Lỗi",JOptionPane.OK_OPTION);
						tfphone.setText("");
						tfphone.setBackground(Color.red);
						 a =1;
					}
					if(CheckString.PhoneNumber(tffax.getText())==false && a == 0){					
						JOptionPane.showMessageDialog(btnOK, "Lỗi nhập fax ! \n Vui lòng nhập lại.","Lỗi",JOptionPane.OK_OPTION);
						tffax.setText("");
						tffax.setBackground(Color.red);
						 a =1;
					}

					if(CheckString.OnlyNumber(tfsotaikhoan.getText(), 15, 6)==false && a == 0){					
						JOptionPane.showMessageDialog(btnOK, "Lỗi nhập số tài khoản ! \n Vui lòng nhập lại.","Lỗi",JOptionPane.OK_OPTION);
						tfsotaikhoan.setText("");
						tfsotaikhoan.setBackground(Color.red);
						 a =1;
					}
					
					
					if ( a !=1 )
					{
					DSJ_KhachHang ikhachhang = new DSJ_KhachHang(); 
					ikhachhang.setSttKH(tfmaKH.getText());
					ikhachhang.setHoTenKH(tftenKH.getText());
					ikhachhang.setDiaChi(tfdiachi.getText());
					ikhachhang.setDienThoai(tfphone.getText());
					ikhachhang.setFax(tffax.getText());
					ikhachhang.setEmail(tfemail.getText());
					ikhachhang.setSoTaiKhoan(tfsotaikhoan.getText());
					ikhachhang.setTenNH(comboBox1.getSelectedItem().toString());
					DSJ_KhachHang.insertInto(ikhachhang);
					}
			
				}
			}
		});
		panel.add(btnOK);
		
		btnCancel = new JButton("Cancel");
		btnCancel.setHorizontalAlignment(SwingConstants.RIGHT);
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		panel.add(btnCancel);
		
		DbselectNganHang();
	}
	
	public void DbselectNganHang(){
		DSJ_KhachHang data = new DSJ_KhachHang();
		Vector<DSJ_KhachHang> getnganhang = data.getbank();
		Vector manganhang = new Vector();
		for(int i=0;i<getnganhang.size();i++){
			comboBox.addItem(getnganhang.get(i).getTenNH());
			comboBox1.addItem(getnganhang.get(i).getMaNH());
		}
	}
	
	
}
